package com.qiye.pointsserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qiye.pointsserver.common.PageUtil.PageParam;
import com.qiye.pointsserver.common.PageUtil.PageResult;
import com.qiye.pointsserver.common.result.Result;
import com.qiye.pointsserver.model.dto.AutherDto;
import com.qiye.pointsserver.model.entity.Auther;
import com.qiye.pointsserver.service.AutherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "05.其他积分模块")
@RestController
@RequestMapping("/api/v1/auther")
@RequiredArgsConstructor
@Slf4j
public class AutherController {

    @Resource
    private AutherService autherService;

    @Operation(summary = "添加其他资源积分")
    @PostMapping("/add")
    public Result add(@RequestBody AutherDto autherDto){
        LambdaQueryWrapper<Auther> autherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        autherLambdaQueryWrapper.eq(Auther::getType, autherDto.getType());
        if (autherService.count(autherLambdaQueryWrapper) > 0){
            return Result.failed("该类型已存在");
        }
        Auther auther = new Auther();
        auther.setType(autherDto.getType());
        auther.setPoints(autherDto.getPoints());
        auther.setTime(autherDto.getTime());
        auther.setDescribe(autherDto.getDescribe());
        autherService.save(auther);
        return Result.success("添加成功");
    }

    @Operation(summary = "修改其他资源积分")
    @PostMapping("/update")
    public Result update(@RequestBody AutherDto autherDto){
        LambdaUpdateWrapper<Auther> autherLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        autherLambdaUpdateWrapper.eq(Auther::getId, autherDto.getId());
        if (autherDto.getPoints() != null){
            autherLambdaUpdateWrapper.set(Auther::getPoints, autherDto.getPoints());
        }
        if (autherDto.getTime() != null){
            autherLambdaUpdateWrapper.set(Auther::getTime, autherDto.getTime());
        }
        if (autherDto.getType() != null){
            autherLambdaUpdateWrapper.set(Auther::getType, autherDto.getType());
        }
        if (autherDto.getDescribe() != null){
            autherLambdaUpdateWrapper.set(Auther::getDescribe, autherDto.getDescribe());
        }
        autherService.update(autherLambdaUpdateWrapper);
        return Result.success("修改成功");
    }

    @Operation(summary = "删除其他资源积分")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        autherService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "查询其他资源积分")
    @GetMapping("/query/list")
    public Result query(){
        List<AutherDto> autherDtos = autherService.search();
        return Result.success(autherDtos);
    }

    @Operation(summary = "关键词分页查询")
    @PostMapping("/search")
    public Result searcAuther(@RequestBody PageParam pageParam) {
        PageResult<Auther> autherList = autherService.searchAuther(pageParam);
        return Result.success(autherList);
    }

}
